package com.ashley.day_0607;
//二分查找
public class ArrayTest6 {

	public static void main(String[] args) {
		int arr[] = new int[] { -5, 12, 34, 78, 99, 127, 564, 682, 998 };// 必须保证所要查找的对象是有序的
		int dest = 34;
		int head = 0;
		int end = arr.length - 1;
		boolean isFlag = true;
		while (head <= end) {
			int mid = (head + end) / 2;
			if (dest == arr[mid]) {
				System.out.println("找到了指定元素，其位置为：" + mid);
				isFlag = false;
				break;
			} else if (dest < arr[mid]) {
				end = mid - 1;
			} else {
				head = mid + 1;
			}
		}
		if (isFlag) {
			System.out.println("没有找到指定元素！");
		}
	}
}